﻿using System;
using System.Text;
using System.Windows.Forms;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQ.Client.Framing;

namespace EasyMQ
{
    public partial class Form1 : Form
    {
        private string queueWeb = "EasyMQWeb";

        private string queue = "EasyMQ";

        private Action<string> SetText;

        private static readonly Form1 _mainForm;
        static Form1()
        {
            _mainForm = new Form1();
        }

        private Form1()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            AcceptMessage();
            AcceptMessageWeb();
            SetText += OnSetText;
        }

        /// <summary>
        /// 主窗口单列
        /// </summary>
        public static Form1 SingleForm
        {
            get { return _mainForm; }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Send();
        }

        #region 发送消息

        public void Send()
        {
            if (this.IsWeb.Checked)
            {
                SendMessageWeb();
            }
            else
            {
                SendMessage();
            }
        }

        /// <summary>
        /// 发送一条消息(Web管理页面提前创建列队)
        /// </summary>
        public void SendMessageWeb()
        {
            var connectionFactory = new ConnectionFactory { HostName = "localhost",UserName="twb",Password="twb0223" };
            using (var connection = connectionFactory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    BasicProperties basicProperties = new BasicProperties { Persistent = true };
                    var payload = Encoding.UTF8.GetBytes(textBox1.Text);

                    channel.BasicPublish("", queueWeb, basicProperties, payload);
                }
            }
        }

        /// <summary>
        /// 发送一条消息（手动创建列队）
        /// </summary>
        public void SendMessage()
        {
            try
            {
                var connectionFactory = new ConnectionFactory { HostName = "localhost", UserName = "twb", Password = "twb0223" };
                using (var connection = connectionFactory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        //channel.QueueDeclare(queue, false, false, false, null);
                        BasicProperties basicProperties = new BasicProperties { Persistent = true };
                        var payload = Encoding.UTF8.GetBytes(textBox1.Text);

                        channel.BasicPublish("", queue, basicProperties, payload);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        #endregion

        #region 接受消息



        /// <summary>
        /// 接受消息
        /// </summary>
        public void AcceptMessage()
        {
            ConnectionFactory connectionFactory1 = new ConnectionFactory { HostName = "localhost", UserName = "twb", Password = "twb0223" };
            var connection = connectionFactory1.CreateConnection();
            var channel = connection.CreateModel();

            var consumer = new EventingBasicConsumer(channel);

            channel.QueueDeclare(queue, true, false, false, null);

            consumer.Received += (ch, deliver) =>
            {
                string jsonString = Encoding.UTF8.GetString(deliver.Body);
                textBox2.Invoke(SetText, jsonString);
            };
            channel.BasicConsume(queue, true, consumer);
        }

        /// <summary>
        /// 接受消息
        /// </summary>
        public void AcceptMessageWeb()
        {
            ConnectionFactory connectionFactory1 = new ConnectionFactory { HostName = "localhost", UserName = "twb", Password = "twb0223" };
            var connection = connectionFactory1.CreateConnection();
            var channel = connection.CreateModel();

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (ch, deliver) =>
            {
                string jsonString = Encoding.UTF8.GetString(deliver.Body);
                textBox2.Invoke(SetText, jsonString);
            };
            channel.BasicConsume(queueWeb, true, consumer);
        }

        public void OnSetText(string text)
        {
            if (this.IsWeb.Checked)
            {
                textBox2.Text += string.Format("{0}\r\n", text);
            }
            else
            {
                textBox3.Text += string.Format("{0}\r\n", text);
            }
        }

        #endregion


    }
}
